package com.february.todo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.facebook.stetho.Stetho;
import com.february.todo.DataBase.DataBaseOperate;
import com.february.todo.MyCalendar.CalendarWidget;
import com.february.todo.MyCalendar.SimpleTodoListAdapter;
import com.february.todo.base.activity.BaseActivity;
import com.february.todo.tools.Bean_todo;
import com.february.todo.tools.NavigateBar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MainActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    CalendarWidget calendarWidget;
    NavigateBar navigateBar;

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        setStatusBarDarkMode();
        Stetho.initializeWithDefaults(this);

        calendarWidget = new CalendarWidget(MainActivity.this);

        calendarWidget.mTextMonthDay = findViewById(R.id.tv_month_day);
        calendarWidget.mTextYear = findViewById(R.id.tv_year);
        calendarWidget.mTextLunar = findViewById(R.id.tv_lunar);
        calendarWidget.mTextCurrentDay = findViewById(R.id.tv_current_day);
        calendarWidget.mImgToday = findViewById(R.id.ib_calendar);
        calendarWidget.mCalendarView = findViewById(R.id.calendarView);
        calendarWidget.mCalendarLayout = findViewById(R.id.calendarLayout);
        calendarWidget.recyclerView = findViewById(R.id.recyclerView);

        calendarWidget.initView();
        navigateBar = findViewById(R.id.navigateBar);
        navigateBar.setContext(MainActivity.this);

        FloatingActionButton floatingButton = findViewById(R.id.floatingButton);
        floatingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddTodoActivity.class);
                startActivityForResult(intent, 2);
            }
        });

        calendarWidget.mAdapter.setOnItemLongClickListener(new SimpleTodoListAdapter.RecyclerViewOnItemLongClickListener() {
            @Override
            public boolean onItemLongClickListener(View view, int todoID) {
                Intent intent = new Intent(MainActivity.this, AddTodoActivity.class);
                Bundle bundle = new Bundle();
                Bean_todo t = new DataBaseOperate(MainActivity.this).gatSelectedTodo(todoID);
                bundle.putSerializable("bean_todo", t);
                intent.putExtras(bundle);
                startActivityForResult(intent, 3);
                return true;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                Bean_todo bean_todo = (Bean_todo) data.getSerializableExtra("bean_todo");
                DataBaseOperate DBoprate = new DataBaseOperate(MainActivity.this);
                DBoprate.insertTodo(bean_todo);
                calendarWidget.UpdateTodoList(bean_todo.getDate());
                calendarWidget.UpdateCalendar();
            }
        }

        if (requestCode == 3) {
            if (resultCode == RESULT_OK) {
                Bean_todo bean_todo = (Bean_todo) data.getSerializableExtra("bean_todo");
                DataBaseOperate DBoprate = new DataBaseOperate(MainActivity.this);
                DBoprate.updateTodo(bean_todo);
                calendarWidget.UpdateTodoList(bean_todo.getDate());
                calendarWidget.UpdateCalendar();
            }
        }
    }

    @Override
    protected void initData() {
        calendarWidget.initData();
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
